//
//  MOEditorViewController_iPad.m
//  MOED, Version 1.0
//
//  http://moed.googlecode.com/
//
//  Copyright (c) 2011, Eternity Technologies.
//  All rights reserved.
// 
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions are met:
//
//    * Redistributions of source code must retain the above copyright
//      notice, this list of conditions and the following disclaimer.
//    * Redistributions in binary form must reproduce the above copyright
//      notice, this list of conditions and the following disclaimer in the
//      documentation and/or other materials provided with the distribution.
//    * Neither the name of the Eternity Technologies nor the
//      names of its contributors may be used to endorse or promote products
//      derived from this software without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY ETERNITY TECHNOLOGIES ''AS IS'' AND ANY
//  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
//  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
//  DISCLAIMED. IN NO EVENT SHALL ETERNITY TECHNOLOGIES BE LIABLE FOR ANY
//  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
//  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
//  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
//  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
//  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
//  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//


#import "MOEditorViewController_iPad.h"

#import "MOFormDescription.h"
#import "MOFormSection.h"
#import "MOFormField.h"

#import "MOEditorTableViewCell_iPad.h"
#import "MOPickerViewController_iPad.h"
#import "MOAssociationPickerViewController.h"

#import "MOLongTextViewController_iPad.h"

@implementation MOEditorViewController_iPad

@synthesize xibEditorTableViewCell;

+ (void) presentModalEditor:(MOEditorViewController_iPad *) editorViewController
		   inViewController:(UIViewController *) hostViewController 				  
				  tintColor:(UIColor *) tintColor 
{
	
	UINavigationController *editorViewNavigationController = [[UINavigationController alloc] initWithRootViewController:editorViewController];
	
	/* Tint color, either set by user or framework */
	
	if(tintColor) {
		editorViewNavigationController.navigationBar.tintColor = tintColor;
		editorViewNavigationController.toolbar.tintColor = tintColor;
	}
	else {
		editorViewNavigationController.navigationBar.tintColor = [UIColor colorWithRed:0.0 green:144.0/255.0 blue:199/255.0 alpha:1.0];
		editorViewNavigationController.toolbar.tintColor = [UIColor colorWithRed:0.0 green:144.0/255.0 blue:199/255.0 alpha:1.0];
	}
	
	editorViewNavigationController.modalPresentationStyle = UIModalPresentationFormSheet;
	editorViewNavigationController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
	
	[hostViewController presentModalViewController:editorViewNavigationController animated:YES];
	[editorViewNavigationController release];
	
}

- (id) initWithFormDescription:(MOFormDescription *)sourceFormDescription {
	self = [super initWithFormDescription:sourceFormDescription];
	if(self){
		self.tableView.rowHeight = 66.0;
	}
	return self;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

- (void) dealloc {
	[xibEditorTableViewCell release];
	[super dealloc];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	static NSString *CellIdentifier = @"MOEditorFieldCell";
    
	MOEditorTableViewCell_iPad *cell = (MOEditorTableViewCell_iPad *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) {
		[[NSBundle mainBundle] loadNibNamed:@"MOEditorTableViewCell_iPad" owner:self options:nil];
		cell = xibEditorTableViewCell;
		self.xibEditorTableViewCell = nil;
	}

	MOFormField *formFieldInfo = [formDescription formFieldAtIndexPath:indexPath];
	[cell configureCellFromFormField:formFieldInfo];
	
	return cell;

}


#pragma mark -
#pragma mark TableView Delegate

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	
	MOFormField *formFieldInfo = [formDescription formFieldAtIndexPath:indexPath];
	MOEditorTableViewCell_iPad *editorCell = (MOEditorTableViewCell_iPad *)[tableView cellForRowAtIndexPath:indexPath];
	
	if(self.readOnly && formFieldInfo.longTextModeInReadOnly) {
		
		MOLongTextViewController_iPad *textViewController = nil;
		textViewController = [[MOLongTextViewController_iPad alloc] init];
		
		textViewController.displayText = editorCell.textField.text;
		textViewController.title = formFieldInfo.labelText;
		
		[self.navigationController pushViewController:textViewController animated:YES];
		[textViewController release];
		
	}

	if(formFieldInfo.fieldType == MOFieldTypeModel) {
		
		MOAssociationPickerViewController *associationPickerViewController = nil;
		associationPickerViewController = [[MOAssociationPickerViewController alloc] initWithNibName:@"MOAssociationPickerViewController" bundle:nil];
		
		associationPickerViewController.managedObjectContext = self.managedObjectContext;
		associationPickerViewController.targetObject = self.managedObject;
		associationPickerViewController.formField = formFieldInfo;
		associationPickerViewController.editorTableViewCell = editorCell;
		
		[self presentAccessoryViewControllerInPopoverController:associationPickerViewController cellFrame:editorCell.frame];
		[associationPickerViewController release];
		return;
				
	}
	
	if(formFieldInfo.options != nil) {
		
		MOPickerViewController_iPad *pickerViewController = nil;
		pickerViewController = [[MOPickerViewController_iPad alloc] initWithNibName:@"MOPickerViewController_iPad" bundle:nil];
		pickerViewController.contentSizeForViewInPopover = CGSizeMake(360, 216);

		pickerViewController.managedObject = self.managedObject;
		pickerViewController.tableViewCell = editorCell;
		pickerViewController.formField = formFieldInfo;
		
		[self presentAccessoryViewControllerInPopoverController:pickerViewController cellFrame:editorCell.frame];

		[pickerViewController release];
		return;
		
	}
	
	/* Else enable the textbox for editing */
	[editorCell.textField becomeFirstResponder];
	
}

- (void) presentAccessoryViewControllerInPopoverController:(UIViewController *) accessoryViewController cellFrame:(CGRect) frame {
	
	UINavigationController *accessoryNavigationController = nil;
	accessoryNavigationController = [[UINavigationController alloc] initWithRootViewController:accessoryViewController];
	
	/* Present it in popover controller */
	UIPopoverController *selectionBoxPopover = nil;
	selectionBoxPopover = [[UIPopoverController alloc] initWithContentViewController:accessoryNavigationController];
	selectionBoxPopover.delegate = self;
	
	[accessoryNavigationController release];
	
	/* Show the pop over */
	[selectionBoxPopover presentPopoverFromRect:frame 
										 inView:self.view 
					   permittedArrowDirections:UIPopoverArrowDirectionAny 
									   animated:YES];
	
}

#pragma mark -
#pragma mark PopoverController delegate


- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
	/* Previous not relesed */
	[popoverController release];
	[self validateManagedObject];
}

@end

